package serializer;

import spi.SpiLoader;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 获取序列化器工厂类
 */
public class SerializerFactory {
    //  1.动态实现
//    private static final String JdkKey = "Jdk";
//    private static final String JsonKey = "Json";
//    private static final String KryoKey = "Kryo";
//    private static final String HessianKey = "Hessian";
//    private static Map<String,Serializer> serializerMap = new ConcurrentHashMap<>();
//    //初始化内置的动态序列化器
//    static {
//        serializerMap.put(JdkKey,new JdkSerializer());
//        serializerMap.put(JsonKey,new JsonSerializer());
//        serializerMap.put(KryoKey,new KryoSerializer());
//        serializerMap.put(HessianKey,new HessianSerializer());
//    }
    // 2.自定义spi,自动加载序列化器接口spi
    static {
        SpiLoader.load(Serializer.class);
    }

    /**
     * 获取实例
     *
     * @param key
     * @return
     */
    public static Serializer getInstance(String key) {
        return SpiLoader.getInstance(Serializer.class, key);
    }
}
